package com.xzz.string;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: hhz
 * @create: 2022-03-01 13:08
 * 6. Z 字形变换
 **/
public class Convert {
    public static void main(String[] args) {
        System.out.println(convert("PAYPALISHIRING",3));
    }
    public static String convert(String s, int numRows) {
        if(numRows<2) return s;
        List<StringBuilder> rows=new ArrayList<>();
        for(int i=0;i<numRows;i++) {
            rows.add(new StringBuilder());
        }
        int i=0,flag=-1;
        for(char c:s.toCharArray()) {
            rows.get(i).append(c);
            if(i==0||i==numRows-1) {
                flag=-flag;
            }
            i+=flag;
        }
        return rows.stream().reduce(StringBuilder::append).get().toString();
    }
}
